package com.cysion.study.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.ApiException
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.NoticeAdapter
import com.cysion.study.adapter.NoticeAdapter.Companion.agree
import com.cysion.study.adapter.NoticeAdapter.Companion.refuse
import com.cysion.study.entity.NoticeEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.presenter.NoticePresenter
import com.cysion.study.ui.iview.NoticeView
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_know_list.*
import kotlinx.android.synthetic.main.activity_notice.*
import kotlinx.android.synthetic.main.activity_notice.multiView
import kotlinx.android.synthetic.main.activity_notice.smartLayout
import kotlinx.android.synthetic.main.activity_notice.topbar

class NoticeActivity : BaseActivity(),  NoticeView {



    //绑定presenter
    private val presenter by lazy {
        NoticePresenter().apply {
            attach(this@NoticeActivity)
        }
    }

    private lateinit var proAdapter: NoticeAdapter
    private val noticeList: MutableList<NoticeEntity> = mutableListOf()
    private var curPage = 1

    override fun getLayoutId(): Int = R.layout.activity_notice

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("通知列表")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()

    }

    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }



    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            presenter.getDataList(curPage)
            smartLayout.setEnableLoadMore(true)
            fl_load_state.visibility= View.GONE
        }
        smartLayout.setOnLoadMoreListener {
            fl_load_state.visibility= View.GONE
            presenter.getDataList(curPage)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rvBloglist.isNestedScrollingEnabled = false
        proAdapter = NoticeAdapter(noticeList, Box.context)
        rvBloglist.adapter = proAdapter
        rvBloglist.layoutManager = LinearLayoutManager(Box.context)

        proAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {
                val bundle = Bundle()
                bundle.putSerializable("obj", obj)
                noticeRead(obj.id.toString(), "")
                startActivity_ex<NoticeDetailActivity>("bundle", bundle)
            }else if (flag == agree) {
                //isConfirm":1, //是否需要确认 0.不需要 1.需要确认
                if (obj.isConfirm == "0") {
                    noticeRead(obj.id.toString(), "")
                } else {
                    noticeRead(obj.id.toString(), "1")
                }

            } else if (flag == refuse) {
                if (obj.isConfirm == "0") {
                    noticeRead(obj.id.toString(), "")
                } else {
                    noticeRead(obj.id.toString(), "2")
                }
            }
        }
    }
    fun noticeRead(nid: String, confirmResult: String) {
        ApiClient.api.noticeRead(
            UserCache.token, nid, confirmResult
        ).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (confirmResult == "1") {
                        ShowToast("已确认")
                    }
                    curPage = 1
                    presenter.getDataList(curPage)
                    Alert.close()
                }
                _onError {
                    //                        Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    curPage = 1
                    presenter.getDataList(curPage)
                }
            }
    }

    //回调接口数据  收益
    override fun onProMoney(notice: MutableList<NoticeEntity>) {
        if (curPage == 1) {
            noticeList.clear()
            if (notice.size == 0) {
                multiView.showEmpty()
                multiView.showEmpty()
            }
        }
        if (notice.size <= 0) {
            smartLayout.setEnableLoadMore(false)
        } else {
            curPage++
            noticeList.addAll(notice)
            proAdapter.notifyDataSetChanged()
            multiView.showContent()
        }

    }

    //出错回调
    override fun onGetProMoneyError(handle: ApiException) {
        fl_load_state.visibility=View.VISIBLE
        tvLoadFinish.visibility=View.VISIBLE
    }

    override fun loading() {
        Alert.loading(this)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)
    }
    override fun closeMvp() {
        presenter.detach()
    }

}
